/**
 * 将文本按中文断句标点分成多段，每n个标点为1段
 * @param text
 * @param n
 * @returns
 */
export function splitTextByPunctuationWithCount(
  text: string,
  n: number,
): string[] {
  // 去掉空格和换行
  const cleanedText = text.replace(/[\s\r\n\\n]+/g, '');

  // 定义中文标点符号
  const chinesePunctuation = ['。', '？', '！', '……'];

  // 根据标点符号索引划分文本
  const result: string[] = [];

  let str = '';
  let index = 0;
  for (let i = 0; i <= cleanedText.length; i++) {
    str = str + cleanedText[i];
    if (chinesePunctuation.includes(cleanedText[i])) {
      index++;
      if (index === n) {
        if (cleanedText[i + 1] && cleanedText[i + 1] === '”') {
          str += cleanedText[i + 1];
          i++;
        }
        result.push(str);
        str = '';
        index = 0;
      }
    }
  }

  // 返回结果数组
  return result.length > 0 ? result : [text];
}

/**
 * 将文本根据换行符号分成多段 ,去掉首尾空格
 */
export function splitTextByLineBreak(text: string): string[] {
  // 去掉空格
  const cleanedText = text.trim();
  return cleanedText.split('\n\n').map((item) => item.trim());
  // return cleanedText.split('      ');
}
